<?php


namespace App\Model;

use Illuminate\Database\Eloquent\Model;


class Category  extends Model {
    protected $table = 'category';
    protected $primaryKey = 'id';
    protected $dates = [
        'created_at',
        'updated_at',
    ];


    /** 获取一级分类
     * @param $id
     * @return mixed
     */
    public static function getCateList(){
        $firstList = Category::where(['status'=>1,"pid"=>0])
            ->get()
            ->toArray();

        foreach ($firstList as &$value){
            $value['list']=self::getList($value['id']);
        }
        return $firstList;
    }


    /** 获取类别的详情信息
     * @param $id
     * @return mixed
     */
    public static function getInfo($id){
        $info = Category::where(['status'=>1,"id"=>$id])
            ->first()
            ->toArray();
        if($info['pid'] == 0){
            $info['list'] = self::getList($id);
        }
        return $info;
    }

    /** 获取指定分类的所有子类
     * @param $id
     * @return mixed
     */
    public static function getList($id){
        return Category::where(['status'=>1,"pid"=>$id])
                ->orderBy('sort', 'asc')
                ->get()
                ->toArray();
    }


    /**
     * 获取这个类别下的所有站点
     */
    public function webs(){
        return $this->hasMany(Webs::class,"cate_id","id");
    }




}
